package com.zxbit.flash.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.bean.AuthInfoData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
    private Context context;
    private List<AuthInfoData.ContactsInfoBean.ContactsInfosBean> data;
    private OnSelectContactListener onSelectContactListener;
    private OnSelectRelationListener onSelectRelationListener;


    private boolean isOPPO;

    public void setModel(boolean model) {
        isOPPO = model;
        notifyDataSetChanged();
    }
    public ContactAdapter(Context context, List<AuthInfoData.ContactsInfoBean.ContactsInfosBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_contact_layout, parent, false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ContactViewHolder holder, int position) {
        holder.tvContact.setText(context.getResources().getString(R.string.contact_auth_contact) + " " + (position+1));


        holder.etName.setText(data.get(position).getUserName());
        holder.etPhone.setText(data.get(position).getMobile());
        holder.etRelation.setText(data.get(position).getRelationValue());
        holder.etRelation.setTag(data.get(position).getRelation());

        holder.etName.setTag(data.get(position).getUserName());

        if (isOPPO) {
            holder.etPhone.setHint(context.getResources().getString(R.string.contact_auth_phone_toast));
            holder.etPhone.setFocusable(true);
            holder.etPhone.setFocusableInTouchMode(true);
            holder.rlPhone.setOnClickListener(null);
            holder.ivPhone.setVisibility(View.INVISIBLE);
        } else {
            holder.ivPhone.setVisibility(View.VISIBLE);
            holder.rlPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSelectContactListener != null && !isOPPO) {
                        onSelectContactListener.onClick(holder.rlPhone, holder.getLayoutPosition());
                    }
                }
            });

            holder.etPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSelectContactListener != null&& !isOPPO) {
                        onSelectContactListener.onClick(holder.etPhone, holder.getLayoutPosition());
                    }
                }
            });
        }

        holder.rlRelation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelectRelationListener != null) {
                    onSelectRelationListener.onClick(holder.etRelation, holder.getLayoutPosition());
                }
            }
        });
        holder.etRelation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelectRelationListener != null) {
                    onSelectRelationListener.onClick(holder.etRelation, holder.getLayoutPosition());
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_contact)
        TextView tvContact;
        @BindView(R.id.etName)
        EditText etName;
        @BindView(R.id.rlName)
        RelativeLayout rlName;
        @BindView(R.id.etPhone)
        EditText etPhone;
        @BindView(R.id.ivPhone)
        ImageView ivPhone;
        @BindView(R.id.rlPhone)
        RelativeLayout rlPhone;
        @BindView(R.id.etRelation)
        TextView etRelation;
        @BindView(R.id.rlRelation)
        RelativeLayout rlRelation;

        public ContactViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnSelectContactListener {
        void onClick(View view, int position);
    }

    public void setOnSelectContactListener(OnSelectContactListener onSelectContactListener) {
        this.onSelectContactListener = onSelectContactListener;
    }


    public interface OnSelectRelationListener {
        void onClick(View view, int position);

    }

    public void setOnSelectRelationListener(OnSelectRelationListener onSelectRelationListener) {
        this.onSelectRelationListener = onSelectRelationListener;
    }


}
